package cn.wolfcode.luowowo.web.controller;

import cn.wolfcode.luowowo.QueryObject.StrategyCommentQuery;
import cn.wolfcode.luowowo.QueryObject.StrategyQuery;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.StrategyTheme;
import cn.wolfcode.luowowo.service.*;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/strategy")
public class StrategyControll {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @RequestMapping("/list")
    public Object list(Model model, @ModelAttribute("qo") StrategyQuery qo) {
        Page<Strategy> page = strategyService.query(qo);
        model.addAttribute("page", page);
        return "/strategy/list";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Strategy strategy) {
        strategyService.saveOrUpdate(strategy);
        return JsonResult.success();
    }


    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        strategyService.delete(id);
        return JsonResult.success();
    }

    @RequestMapping("/input")
    public Object input(Model model, String id) {
        //strategy
        if (StringUtils.hasLength(id)) {
            Strategy strategy = strategyService.get(id);
            model.addAttribute("strategy", strategy);

        }
        //catalogs
        List<CatalogVO> list = strategyCatalogService.groupList();
        model.addAttribute("catalogs", list);

        //theme
        List<StrategyTheme> themes = strategyThemeService.list();
        model.addAttribute("themes", themes);

        return "/strategy/input";
    }

    @RequestMapping("/changeState")
    @ResponseBody
    public Object changeState(int state, String id) {
        strategyService.changeState(state, id);
        return JsonResult.success();
    }

    @RequestMapping("/commentlist")
    public Object commentlist(Model model,StrategyCommentQuery qo) {
        Page<StrategyComment> page = strategyCommentService.query(qo);
        model.addAttribute("page", page);
        return "/strategyCommend/list";
    }

}
